package com.iceframework.core.utils;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import com.iceframework.core.support.cache.ICacheService;

public class KeyUtils {
	
	public final static String PREFIX = "ICE:KEY:SEQ_";
	
	/**
	 * length为主键长度
	 * @param length
	 * @return
	 */
	public static String genKey(String clsName) {
		ICacheService cacheService = SpringContextUtils.getBean("cacheService",ICacheService.class);
		String key = cacheService.get(KeyUtils.PREFIX+clsName);
		if(StringUtils.isNotEmpty(key)) {
			int intKey = Integer.parseInt(JsonUtils.convertString2Obj(key, String.class));
			intKey++;
			cacheService.set(KeyUtils.PREFIX+clsName,String.valueOf(intKey));
			return String.valueOf(intKey);
		}else {
			cacheService.set(KeyUtils.PREFIX+clsName,String.valueOf(1));
			return String.valueOf(1);
		}
	}
	
	/**
	 * length为主键长度
	 * @param length
	 * @return
	 */
	public static String genUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
}
